#include "StatusLamp.h"

#define LON LOW
#define LOFF HIGH

StatusLamp::StatusLamp(uint32_t pin)
    : BaseLamp(pin), _state(_ST_ERROR), taskLookUp(pin), taskRunning(pin), taskError(pin), taskWifiConfig(pin), taskSyncNtp(pin) {}

StatusLamp::~StatusLamp() {}

void StatusLamp::setSt(emST state)
{
    if (_state == state)
        return;

    _state = state;
}

bool StatusLamp::Run()
{

    switch (_state)
    {
    case _ST_LOOKUP_NETWORK:
        taskLookUp.Run();
        break;
    case _ST_RUNNING:
        taskRunning.Run();
        break;
    case _ST_ERROR:
        taskError.Run();
        break;
    case _ST_WIFI_CONFIG:
        taskWifiConfig.Run();
        break;
    case _ST_SYNC_NTP:
        taskSyncNtp.Run();
        break;
    case _ST_STOP:
    default:
        digitalWrite(_pin, LOFF);
        break;
    }
    return false;
}

bool StatusLamp::TaskLookUp::Run()
{
    HT_BEGIN();
    for (;;)
    {
        digitalWrite(_pin, LOFF);
        // _ex_timer.start(300);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(300);
        for (i = 0; i < 3; i++)
        {
            digitalWrite(_pin, LON);
            // _ex_timer.start(80);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(80);
            digitalWrite(_pin, LOFF);
            // _ex_timer.start(80);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(80);
        }
    }
    HT_END();
}

bool StatusLamp::TaskRunning::Run()
{
    HT_BEGIN();
    for (;;)
    {
        digitalWrite(_pin, LOFF);
        // _ex_timer.start(3000);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(3000);
        digitalWrite(_pin, LON);
        // _ex_timer.start(50);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(50);
    }

    HT_END();
}

bool StatusLamp::TaskError::Run()
{
    HT_BEGIN();
    for (;;)
    {
        for (i = 0; i < 3; i++)
        {
            digitalWrite(_pin, LOFF);
            // _ex_timer.start(240);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(240);
            digitalWrite(_pin, LON);
            // _ex_timer.start(240);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(240);
        }
        for (i = 0; i < 3; i++)
        {
            digitalWrite(_pin, LOFF);
            // _ex_timer.start(80);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(80);
            digitalWrite(_pin, LON);
            // _ex_timer.start(80);
            // PT_WAIT_UNTIL(_ex_timer.expired());
            HT_DELAY(80);
        }
    }
    HT_END();
}

bool StatusLamp::TaskWifiConfig::Run()
{
    HT_BEGIN();
    for (;;)
    {
        digitalWrite(_pin, LOFF);
        // _ex_timer.start(240);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(240);
        digitalWrite(_pin, LON);
        // _ex_timer.start(240);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(240);

        digitalWrite(_pin, LOFF);
        // _ex_timer.start(80);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(80);
        digitalWrite(_pin, LON);
        // _ex_timer.start(80);
        // PT_WAIT_UNTIL(_ex_timer.expired());
        HT_DELAY(80);
    }
    HT_END();
}

bool StatusLamp::TaskSyncNtp::Run()
{
    HT_BEGIN();
    for (;;)
    {

        for (i = 0; i < 2; i++)
        {
            digitalWrite(_pin, LOFF);
            HT_DELAY(80);
            digitalWrite(_pin, LON);
            HT_DELAY(80);
        }

        digitalWrite(_pin, LOFF);
        HT_DELAY(1000);
    }
    HT_END();
}
